package lcr;

import java.util.Arrays;

public class Test0146 {
    public static void main(String[] args) {
        int[][] array = new int[][]{{1,2}};
        System.out.println(Arrays.toString(new Solution0146_1().spiralArray(array)));
    }
}

class Solution0146_1 {
    public int[] spiralArray(int[][] array) {
        int length1 = array.length;
        if (length1 == 0) return new int[0];

        int length2 = array[0].length;
        if (length2 == 0) return new int[0];

        int[] ans = new int[length1 * length2];

        int up=0,down= length1,left=-1,right=array[0].length,index = 0;
        while (left<right-1){
            for (int i = left+1; i < right; i++) {
                ans[index++]=array[up][i];
            }

            if (up+1>=down)break;
            for (int i = up+1; i < down; i++) {
                ans[index++]=array[i][right-1];
            }

            if (right-2<=left)break;
            for (int i = right-2; i > left; i--) {
                ans[index++]=array[down-1][i];
            }

            if (down-2<=up) break;
            for (int i = down-2; i > up; i--) {
                ans[index++]=array[i][left+1];
            }
            up++;
            down--;
            left++;
            right--;
        }

        return ans;
    }
}
